<!doctype html><html><head>
    <title>A canvas scale example</title>
    <meta name="DC.creator" content="Kamiel Martinet, http://www.martinet.nl/">
    <meta name="DC.publisher" content="Mozilla Developer Center, http://developer.mozilla.org">

<script src="../uuCanvas.js"></script>

<script>
function xcanvas() {
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}
function matrix() {
    drawShape(document.getElementById('slcanvas'), 1);
    drawShape(document.getElementById('vmlcanvas'), 1);
    drawShape(document.getElementById('flashcanvas'), 1);
}

var globalAlpha = 1.0;
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}

var lineWidth = 1.0;
function switchLineWidth() {
    lineWidth += 2;
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;
//  ctx.lineWidth = lineWidth;

    ctx.strokeStyle = "#fc0";
    ctx.lineWidth = lineWidth + 0.5;
    ctx.fillRect(0,0,300,300);

    // Uniform scaling
    ctx.save()
    ctx.translate(50,50);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(0.75,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(133.333,0);
    ctx.scale(0.75,0.75);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

    // Non uniform scaling (y direction)
    ctx.strokeStyle = "#0cf";
    ctx.save()
    ctx.translate(50,150);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

    // Non uniform scaling (x direction)
    ctx.strokeStyle = "#cf0";
    ctx.save()
    ctx.translate(50,250);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(133.333,0);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(177.777,0);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

}
function drawSpirograph(ctx,R,r,O){
    var x1 = R-O;
    var y1 = 0;
    var i  = 1;
    ctx.beginPath();
    ctx.moveTo(x1,y1);
    do {
        if (i>20000) break;
        var x2 = (R+r)*Math.cos(i*Math.PI/72) - (r+O)*Math.cos(((R+r)/r)*(i*Math.PI/72))
        var y2 = (R+r)*Math.sin(i*Math.PI/72) - (r+O)*Math.sin(((R+r)/r)*(i*Math.PI/72))
        ctx.lineTo(x2,y2);
        x1 = x2;
        y1 = y2;
        i++;
    } while (x2 != R-O && y2 != 0 );
    ctx.stroke();
}
</script>
    <style type="text/css">
      body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
      h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
      canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
      pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
    </style>
  </head>
  <body>
    <h1>A canvas <code>scale</code> example</h1>
    <div>
      <input type="button" value="matrix effect" onclick="matrix()" />
      <input type="button" value="alpha -0.2" onclick="switchAlpha()" />
      <input type="button" value="lineWidth +2" onclick="switchLineWidth()" />
      <br />
      <canvas id="slcanvas" class="sl" width="300" height="300"></canvas>
      <canvas id="vmlcanvas" class="vml" width="300" height="300"></canvas>
      <canvas id="flashcanvas" class="flash" width="300" height="300"></canvas>
      <pre>
function xcanvas() {
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}
function matrix() {
    drawShape(document.getElementById('slcanvas'), 1);
    drawShape(document.getElementById('vmlcanvas'), 1);
    drawShape(document.getElementById('flashcanvas'), 1);
}

var globalAlpha = 1.0;
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}

var lineWidth = 1.0;
function switchLineWidth() {
    lineWidth += 2;
    drawShape(document.getElementById('slcanvas'));
    drawShape(document.getElementById('vmlcanvas'));
    drawShape(document.getElementById('flashcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;
//  ctx.lineWidth = lineWidth;

    ctx.strokeStyle = "#fc0";
    ctx.lineWidth = lineWidth + 0.5;
    ctx.fillRect(0,0,300,300);

    // Uniform scaling
    ctx.save()
    ctx.translate(50,50);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(0.75,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(133.333,0);
    ctx.scale(0.75,0.75);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

    // Non uniform scaling (y direction)
    ctx.strokeStyle = "#0cf";
    ctx.save()
    ctx.translate(50,150);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(100,0);
    ctx.scale(1,0.75);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

    // Non uniform scaling (x direction)
    ctx.strokeStyle = "#cf0";
    ctx.save()
    ctx.translate(50,250);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(133.333,0);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);

    ctx.translate(177.777,0);
    ctx.scale(0.75,1);
    drawSpirograph(ctx,22,6,5);
    ctx.restore();

}
function drawSpirograph(ctx,R,r,O){
    var x1 = R-O;
    var y1 = 0;
    var i  = 1;
    ctx.beginPath();
    ctx.moveTo(x1,y1);
    do {
        if (i>20000) break;
        var x2 = (R+r)*Math.cos(i*Math.PI/72) - (r+O)*Math.cos(((R+r)/r)*(i*Math.PI/72))
        var y2 = (R+r)*Math.sin(i*Math.PI/72) - (r+O)*Math.sin(((R+r)/r)*(i*Math.PI/72))
        ctx.lineTo(x2,y2);
        x1 = x2;
        y1 = y2;
        i++;
    } while (x2 != R-O && y2 != 0 );
    ctx.stroke();
}
      </pre>
    </div>
  </body>
</html>
